package com.chao.common.util.file;

import com.chao.common.util.ListUtile;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.file.FileUpload;
import com.chao.common.util.file.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 静态方法查询文件资源表
 * chao
 * 2020-07-18
 */
@Component
public class SysFile {

    @Autowired
    private com.chao.common.util.file.SysFileService fileService;

    private static SysFileService fileStaticService;

    @PostConstruct
    public void init() {
        fileStaticService = fileService;
    }

    /**
     * 新增文件资源相关
     */
    public static void insertResource(FileUpload fileUpload, String objectId){
        insertResource(fileUpload,objectId,"");
    }

    /**
     * 新增文件资源相关
     */
    public static void insertResource(FileUpload fileUpload, String objectId, String objectField){
        if(null == fileUpload || StrUtile.isEmpty(fileUpload.getFileName())||StrUtile.isEmpty(fileUpload.getFilePrefix())||StrUtile.isEmpty(fileUpload.getFileUrl())){
            return;
        }
        setObj(fileUpload,objectId,objectField);
        fileStaticService.insertResource(fileUpload);
    }

    /**
     * 根据关联id查询单个文件资源
     */
    public static FileUpload getRelation(String objectId){
        List<FileUpload> fileList = getRelationList(objectId,"");
        if(ListUtile.isEmpty(fileList)){
            return null;
        }
        return fileList.get(0);
    }

    /**
     * 根据关联id查询单个文件资源
     */
    public static FileUpload getRelation(String objectId, String objectField){
        List<FileUpload> fileList = getRelationList(objectId,objectField);
        if(ListUtile.isEmpty(fileList)){
            return null;
        }
        return fileList.get(0);
    }

    /**
     * 根据关联id查询文件资源
     */
    public static List<FileUpload> getRelationList(String objectId){
        return getRelationList(objectId,"");
    }

    /**
     * 根据关联id和字段查询文件资源
     */
    public static List<FileUpload> getRelationList(String objectId, String objectField){
        return fileStaticService.selectResourceByRelationId(objectId,objectField).getList();
    }

    /**
     * 根据关联id删除文件资源
     */
    public static void deleteResourceByRelationId(String objectId){
        deleteResourceByRelationId(objectId,"");
    }

    /**
     * 根据关联id删除文件资源
     */
    public static void deleteResourceByRelationId(String objectId,String objectField){
        fileStaticService.deleteResourceByRelationId(objectId,objectField);
    }

    /**
     * 批量新增文件资源
     */
    public static void insertResourceList(List<FileUpload> fileList, String objectId){
        insertResourceList(fileList,objectId,"");
    }

    /**
     * 批量新增文件资源
     */
    public static void insertResourceList(List<FileUpload> fileList, String objectId, String objectField){
        if(ListUtile.isEmpty(fileList)){
            return;
        }
        for(FileUpload fileUpload : fileList){
            setObj(fileUpload,objectId,objectField);
        }
        fileStaticService.insertResourceList(fileList);
    }

    /**
     * 封装附件对象参数
     */
    private static void setObj(FileUpload fileUpload, String objectId, String objectField){
        fileUpload.setCtime(new Date());
        fileUpload.setFileId(StrUtile.getId());
        fileUpload.setFileUser(RequestUtile.getLoginUserId());
        fileUpload.setObjectId(objectId);
        fileUpload.setObjectField(objectField);
        if(!StrUtile.isEmpty(fileUpload.getFileName()) && fileUpload.getFileName().contains(".")){
            String[] s = fileUpload.getFileName().split("\\.");
            fileUpload.setFileType(s[s.length-1].toLowerCase());
        }
    }

    /**
     * 语音转文字上传七牛云
     */
    public static void textToVoice(String text, String objectId, String objectFile){
//        JSONObject json = new JSONObject();
//        json.put("text", text);
//        json.put("objectId", objectId);
//        json.put("objectFile", objectFile);
//        fileStaticService.textToVoice(json);
    }

    /**
     * 查询多附件记录
     * @param objectIds 关联ID集合
     * @param objectField 关联字段
     * @return
     */
    public static Map<String, List<FileUpload>> getRelationsByObjectIds(String[] objectIds, String objectField){
        Map<String, List<FileUpload>> map = new HashMap<>();
        if (objectIds.length > 0){
            List<FileUpload> resources = fileStaticService.selectResourceByObjectIds(objectIds, objectField);
            if(!ListUtile.isEmpty(resources)){
                map = resources.stream().collect(Collectors.groupingBy(FileUpload::getObjectId));
            }
        }
        return map;
    }
}
